var designClass = "design_class", hideClass = "hide_class", browseClass = "browse_class", addClass = "add_class", editClass = "edit_class";
var upWindow = window.parent.frames["zwIframe"];
if (!upWindow) {
    upWindow = window.parent.frames["componentDiv"].frames["zwIframe"];
}
//关闭自定义扩张控件弹出窗口
function fy_dialog_close() {
    window.parent.$(".layui-layer-iframe").parent().find(".layui-layer-close").trigger("click");
}


function getUpFieldsByFieldval(name) {
    return upWindow.$("[fieldval*='displayName:\"" + name + "\"']");
}

function getUpFieldValueByName(name, index) {
    return getUpFieldValue(getUpFieldsByFieldval(name).eq(index ? index : 0));
}

function getUpFieldValue(field) {
    var fieldVal = field.attr("fieldVal");
    var editTag = field.hasClass(editClass);
    var browseTag = field.hasClass(browseClass);
    var addTag = field.hasClass(addClass);
    if (fieldVal != null && typeof (fieldVal) != 'undefined') {
        fieldVal = eval("(" + fieldVal + ")");
    } else {
        return null;
    }
    var obj = new Object();
    obj.displayName = fieldVal.displayName;
    obj.name = fieldVal.name;
    var tag = true;
    switch (fieldVal.inputType) {
        case "text":
        case "textarea":

            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "checkbox":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            }
            break;
        case "radio":
            if (editTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find(":radio:checked").attr("val4cal");
            }
            break;
        case "select":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + " option:selected")
                        .attr("val4cal");
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).attr("val4cal");
            }
            break;
        case "date":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
                if (obj.value != "") {
                    obj.value = obj.value + " 00:00";
                }
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "datetime":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "flowdealoption":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            } else if (addTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "lable":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relationform":
            if (editTag) {
                obj.value = fieldVal.value;
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "relation":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "project":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name).val();
            } else if (browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "member":
        case "multimember":
        case "account":
        case "multiaccount":
        case "department":
        case "multidepartment":
        case "post":
        case "multipost":
        case "level":
        case "multilevel":
            if (editTag) {
                obj.value = field.find("#" + fieldVal.name + "_txt").val();
                obj.id = field.find("#" + fieldVal.name).val().split('|')[1];
            } else if (browseTag) {
                obj.value = field.find("#" + fieldVal.name).text();
            }
            break;
        case "attachment":
        case "image":
        case "document":
            if (editTag || browseTag) {
                var attNames = "";
                var tempField = field.find("#" + fieldVal.name);
                var tempFileId = "";
                if (tempField[0].tagName.toLowerCase() == "input") {
                    tempFileId = tempField.val();
                } else if (tempField[0].tagName.toLowerCase() == "span") {
                    tempFileId = tempField[0].innerHTML;
                }
                var atts = upWindow.getAttBySubreference(tempFileId);
                for (var j = 0; j < atts.length; j++) {
                    attNames += atts[j].filename + ",";
                }
                if (attNames != "") {
                    attNames = attNames.substr(0, attNames.length - 1);
                }
                obj.value = attNames;
            }
            break;
        case "outwrite":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "externalwrite-ahead":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "exchangetask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        case "querytask":
            if (editTag || browseTag) {
                obj.value = fieldVal.value;
            }
            break;
        default:
            tag = false;
    }
    if (obj.value != undefined) {
        obj.value = obj.value.replace("\r\n", ";");
        obj.value = obj.value.replace("\n", ";");
    }
    return obj;
}


function setUpFieldValue2(field, value, displayValue, index) {

    try {

        var fieldVal = field.attr("fieldVal");
        var editTag = field.hasClass(editClass);
        var addTag = field.hasClass(addClass);
        value = (value);
        displayValue = displayValue ? (displayValue) : displayValue;
        if (fieldVal != null && typeof (fieldVal) != 'undefined') {
            fieldVal = eval("(" + fieldVal + ")");
        } else {
            return null;
        }
        var tag = true;
        switch (fieldVal.inputType) {
            case "text":
            case "textarea":
                if (editTag) {
                    var inField = field.find("#" + fieldVal.name);
                    inField.val(value);
                    inField.trigger("blur");
                } else if (addTag) {
                    var inField = field.find("#" + fieldVal.name);
                    inField.val(value);
                    inField.trigger("blur");
                }
                break;
            case "checkbox":
                if (editTag) {
                    field.find("#" + fieldVal.name).attr("checked", true);
                    field.find("#" + fieldVal.name).trigger("click");
                }
                break;
            case "radio":
                if (editTag) {
                    field.find(":radio").eq(index).attr("checked", true);
                    field.find(":radio").eq(index).trigger("click");
                }
                break;
            case "select":
                if (editTag) {
                    var options = field.find("#" + fieldVal.name + " option");
                    for (var k = 0; k < options.length; k++) {
                        if (options.eq(k).text() == value)
                            options.eq(k).attr("selected", true);
                    }
                    field.find("#" + fieldVal.name).trigger("change");
                }
                break;
            case "date":
                if (editTag) {
                    field.find("#" + fieldVal.name).val(value);
                    field.find("#" + fieldVal.name).trigger("blur");
                }
                break;
            case "datetime":
                if (editTag) {
                    field.find("#" + fieldVal.name).val(value);
                    field.find("#" + fieldVal.name).trigger("blur");
                }
                break;
            case "member":
            case "multimember":
            case "account":
            case "multiaccount":
            case "department":
            case "multidepartment":
            case "post":
            case "multipost":
            case "level":
            case "multilevel":
                if (editTag) {
//				alert(editTag);
                    field.find("#" + fieldVal.name).val(value);
                }
                break;
        }
    } catch (e) {
        // TODO: handle exception
    }
}

function setUpFieldValueByName(name, value, index) {
    var fields = getUpFieldsByFieldval(name);
    setUpFieldValue2(fields.eq(index ? index : 0), value);
}

var fieldMap = {
    "申请人": "name",
    "员工编码": "code",
    "进入福耀时间": "joinDate",
    "所在制造部": "corp",
    "部门": "dept",
    "岗位": "post",
//	"身份证" : "IDCard",
    "联系电话": "tel"
//	"家庭地址" : "address"
};

function onEnterKeyPress() {
    if (event.keyCode == 13) {
        onButtonClick();
    }
}
;

function onButtonClick() {
    var value = document.getElementById("code").value.replace(/ /g, '');
    if (value === '') {
        alert("人员工号不能为空。");
    } else if (value.length != 6) {
        alert("人员工号为六位数字。");
    } else {
        document.getElementById("lodingDiv").style.display = "";
        var xmlhttp = null;
        if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome,
            // Opera,
            xmlhttp = new XMLHttpRequest();
        } else {// code for IE6, IE5
            xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
        }
        xmlhttp.onreadystatechange = function () {
            if (xmlhttp.readyState == 4) {
                if (xmlhttp.status == 200) {
                    text = xmlhttp.responseText;
                    if (text == 1) {
                        alert("人员工号不能为空");
                    } else if (text == 2) {
                        alert("数据库连接异常，请重试或者联系OA系统管理员");
                    } else if (text == 1) {
                        alert("该人员不存在或者HR账号已被停用");
                    } else {
                        try {
                            var values = eval("(" + text + ")");
                            for (var fieldName in fieldMap) {
                                var value = values[fieldMap[fieldName]];
                                setUpFieldValueByName(fieldName, value ? value
                                        : '');
                            }
                            fy_dialog_close();
                        } catch (e) {
                            alert("返回数据格式不对，请重试");
                        }
                    }

                } else {
                    alert("连接服务器异常");
                }
                document.getElementById("lodingDiv").style.display = "none";
            }
        };
        xmlhttp.open("get",
                "../../../oa2hrController.do?method=querMemberInfoByCode&code="
                + value + "&temp=" + Math.random(), true);
        xmlhttp.send();
    }
}
;